<template>
  <div class="rd-flex-1 rd-box-frow">
    <div class="rd-flex-1 rd-pr-12">
      <div class="rd-content rd-area-none">
        <div>search</div>
        <div class="rd-flex-1">
          <p v-for="item in projects" :key="item.id" @click="goProject(item)">{{item.year}} - {{item.name}}</p>
        </div>
      </div>
    </div>
    <div class="rd-pl-12 rd-box-fcol">
      <div class="rd-flex-1 rd-pb-12">
        <div class="rd-content rd-area-y">
          单位个人信息
        </div>
      </div>
      <div class="rd-pt-12">
        <div class="rd-content">
          今日统计
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, ref, toRaw } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { Tools } from '@/utils'
// import LayoutDrawerList from '@/layout/LayoutDrawerList.vue'
import { fetchMyProjects } from '@/api'

const { getMainRoute } = Tools

export default {
  components: {
    // LayoutDrawerList
  },
  setup () {
    const router = useRouter()
    const store = useStore()
    const projects = ref([])
    const projectsSearch = reactive({
      name: '',
      pageNo: 1,
      pageSize: 10,
      projectType: 1
    })

    /**
     * 获取项目列表
     */
    const getProjects = async () => {
      const projectsRes = await fetchMyProjects(toRaw(projectsSearch))
      if (projectsRes.code === 200) {
        projects.value = projectsRes.data
        return
      }
      console.log(projectsRes)
    }
    /**
     * 前往某项目
     */
    const goProject = async item => {
      store.commit('Menus/setMainWait', true)
      store.commit('Menus/setSubjectId', '2')
      const projectId = item.id
      localStorage.setItem('projectId', projectId)
      const mainRoute = await getMainRoute({ projectId })
      store.commit('Tabs/setMainPanes', null)
      router.addRoute(mainRoute)
      store.commit('Menus/setMainWait', false)
      if (mainRoute.children.length) {
        const thePath = mainRoute.children[0].path
        router.push(thePath)
      }
    }

    onMounted(getProjects())

    return {
      projects,
      projectsSearch,
      getProjects,
      goProject
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
